package gw.com.android.ui.quotesearch.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.model.DataManager;
import gw.com.android.ui.BaseActivity;
import gw.com.android.utils.ServerConnnectUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.RecyclerClickListener;


/**
 * 搜索适配器
 * Created by hubble on 2017/3/1.
 */

public class HotProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private BaseActivity mContext;
    private DataItemResult mDataList;
    private RecyclerClickListener mListener;

    public HotProductAdapter(BaseActivity context, DataItemResult mDataList) {
        this.mContext = context;
        this.mDataList = mDataList;
    }

    /**
     * 设置点击事件监听对象
     * @param listener
     */
    public void setRecyclerClickListener(RecyclerClickListener listener){
        this.mListener = listener;
    }

    public void updateData(DataItemResult mDataList) {
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    public void addData(DataItemResult mDataList) {
        if(mDataList == null) {
            this.mDataList = mDataList;
        }else {
            this.mDataList.appendItems(mDataList);
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh;
        switch (viewType) {
            default:
                vh = new SimpleViewHolder(mContext.getLayoutInflater().inflate(R.layout.list_item_quote_hot_search, parent, false));
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        // 填充数据
        SimpleViewHolder mHolder = (SimpleViewHolder) holder;

        final DataItemDetail item = mDataList.getItem(position);

        mHolder.mTvCNName.setText(DataManager.instance().getPrdName(item));
        mHolder.mTvEGName.setText(item.getString(GTSConst.JSON_KEY_SUBSYMBOLNAME));

        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(CommonUtils.isFastDoubleClick()){
                    return;
                }
                if (!NetworkMonitor.hasNetWork()) {
                    return;
                }
                if(!ServerConnnectUtil.instance().isTradeConnect || !ServerConnnectUtil.instance().isQuoteConnect){
                    ServerConnnectUtil.instance().reConnectServerConfirm(mContext);
                    return;
                }
                ActivityManager.showChartActivity(mContext, item.getInt(GTSConst.JSON_KEY_CODEID), item.getInt(GTSConst.JSON_KEY_ZONE), AppContances.TRADE_TYPE_FORM_SEARCH);
                if(null != mListener){
                    mListener.onClick(0, item);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.getDataCount();
    }

    /**
     * 当recylerView 中有不止一种View类型需要展示时， 可通过重写该方法指明每一个位置需要的View的类型</br>
     * 该方法默认返回0， 代表只有单一的view类型</br>
     * 建议：</br>
     * 可用不同的view的布局资源id来标识不同的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvCNName;
        private TextView mTvEGName;
        public SimpleViewHolder(final View itemView) {
            super(itemView);
            mTvCNName = (TextView) itemView.findViewById(R.id.tv_cn_name);
            mTvEGName = (TextView) itemView.findViewById(R.id.tv_eg_name);

        }
    }
}
